package com.manager.car.action;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.omg.CORBA.Request;

import com.google.gson.Gson;
import com.manager.car.po.MessageInfo;
import com.manager.car.service.IMessageService;
import com.manager.car.util.Page;
import com.manager.car.util.Verification;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;




public class MessageAction extends ActionSupport  {

	private static final long serialVersionUID = 1L;
	
	private IMessageService megServiec;//实现接口
	private MessageInfo message;
	private int  page;//当前页
	private int limit;//每页条数
    private String code; //验证码
    private Integer id;//ID
    private String msgContext;//内容查询
    
    
    HttpSession session = ServletActionContext.getRequest().getSession();
    HttpServletResponse response = ServletActionContext.getResponse();
          
	@Override//验证码
	public String execute() throws Exception {
        response.setContentType("image/jpg");
		String code = Verification.getRandCode(4);
		System.out.println(code + "@t434235ddd");
		BufferedImage image = Verification.getImage(100, 30, code, 5);
		//验证码存入session中
		session.setAttribute("Yz", code);
		ImageIO.write(image, "jpg", ServletActionContext.getResponse().getOutputStream());
		//ServletActionContext.getResponse().getWriter().print(code);
		return NONE;

	}
	//读取验证码
	public void queryYz() throws IOException {
		String str  = (String) session.getAttribute("Yz");
		response.getWriter().println(str);
	}
	//前台查询
	public String qtQuery() throws IOException {
		List<MessageInfo> queryAll = megServiec.queryAll("s", 10, 1);
//		Gson gson=new Gson();
//		String json = gson.toJson(queryAll);
		ActionContext.getContext().put("msg", queryAll);
		//response.getWriter().println(json);
		return "qtQ";
		
	}
	//添加留言
	public void addMsg() throws IOException {
		//当前时间
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
		
		message.setMsgTime(format.format(new Date()));
		megServiec.addMeg(message);
		response.getWriter().println(true);
		
	}
	//后台查询全部
	public void queryAll() throws IOException {
		System.out.println("fsljd");
		response.setCharacterEncoding("utf-8");
		//查询数据
		if(msgContext!=null) {
			msgContext=msgContext+"s";
		}
		System.out.println(msgContext+":suibian");
		List<MessageInfo> queryAll = megServiec.queryAll(msgContext, limit, page);
		//查询总记录数
		int count=megServiec.getcount(msgContext, limit, page);
		Page< MessageInfo> pg=new Page<MessageInfo>();
		pg.setDate(queryAll);
		pg.setCount(count);
		pg.setCode(0);
		pg.setMsg("成功");
		Gson gson =new Gson();
		String json = gson.toJson(pg);
		response.getWriter().print(json);
	}
	//修改是否显示(改为1 不显示)
	public void msgUps() throws IOException {
		message= megServiec.queryByMegId(id);
		message.setMsgStatic(1);
		megServiec.updateMeg(message);
		response.getWriter().print(true);
	}
	//删除评论
	public void msgDle() throws IOException {
		megServiec.deleteMeg(id);
		response.getWriter().print(true);
	}
	//后台ID查询
	public String queryId() {
		MessageInfo queryByMegId = megServiec.queryByMegId(id);
		String time= queryByMegId.getMsgTime().replace(" ","T");
		queryByMegId.setMsgTime(time);
		ActionContext.getContext().put("message", queryByMegId);
		return "ById";
	}
	public IMessageService getMegServiec() {
		return megServiec;
	}
	public void setMegServiec(IMessageService megServiec) {
		this.megServiec = megServiec;
	}
	public MessageInfo getMessage() {
		return message;
	}
	public void setMessage(MessageInfo message) {
		this.message = message;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getMsgContext() {
		return msgContext;
	}
	public void setMsgContext(String msgContext) {
		this.msgContext = msgContext;
	}
	public HttpSession getSession() {
		return session;
	}
	public void setSession(HttpSession session) {
		this.session = session;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

}
